package com.web_bloks.project.controllers.client;

import com.google.gwt.user.client.ui.Panel;

import com.web_bloks.system.client.Logger;

import com.google.gwt.user.client.Element;
import com.web_bloks.document.client.Document;
import com.web_bloks.document.client.DocumentListener;

import com.web_bloks.widget.client.WidgetFactory;
import com.web_bloks.widget.client.WidgetManager;

/**
 * Waits for a new document to be valid & rendered
 *  and creates all the 'widgets' from the mark-up
 * 
 * @author Jean-Lou Dupont
 *
 */
public class WidgetController
	implements DocumentListener {

	Panel panel = null;
	
	String collectionName = null;
	
	public WidgetController() {
	}
	/*****************************************
	 * CONFIGURATION
	 *****************************************/

	public void setPanel(Panel p) {
		panel = p;
	}

	public void setWidgetCollectionName(String collectionName) {
		this.collectionName = new String( collectionName );
	}
	/*****************************************
	 * EVENT HANDLERS
	 *****************************************/
	public void onDocumentChanging(String name, String targetRegionName) {
		Logger.logInfo(getClass()+".onDocumentChanging");
		WidgetManager wm = WidgetManager.create();
		wm.clear(collectionName);
	}
	
	public boolean onDocumentChanged(Document doc) {
		//we are not the one rendering the document
		return false;
	}
	
	/**
	 * Goes through the Element tree to find
	 * widgets to create
	 */
	public void onDocumentRendered(Document doc, Element el) {
		Logger.logInfo(getClass()+".onDocumentRendered");
		
		WidgetFactory.createAllFromNode(collectionName, panel, el);
	}

	public void onDocumentRendered(Document doc) {
		// TODO Auto-generated method stub
		
	}
	
}//
